<?php

namespace App\Models;

use App\Scopes\WarehouseScope;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class Delivery extends Model
{
    const TABLE = 'yt_delivery';
    protected $table = self::TABLE;

    /**
     * 模型的 "booted" 方法
     *
     * @return void
     */
    /*protected static function booted()
    {
        static::addGlobalScope(new WarehouseScope());
    }*/


    //运输方式:1=海运;2=海派;3=空派;4=快递;5=海卡
    const TRANSPORT_TYPE = [
        'SEA'          => 1,    //海运
        'SEA_DELIVERY' => 2,    //海派
        'AIR'          => 3,    //空派
        'EXPRESS'      => 4,    //快递
        'SEA_CAR'      => 5,    //海卡
    ];

    //审核状态
    const CHECK_STATUS = [
        'UNSUBMITTED'     => 0,    //未提交
        'WAITTING'        => 1,    //待初审
        'NORMAL'          => 2,    //初审通过
        'REJECT'          => 3,    //初审驳回
        'REVIEW_NORMAL'   => 4,    //复核通过
        'REVIEW_REJECT'   => 5,    //复核驳回
        'CONFIRM_CABINET' => 6,    //确认装柜
        'RETURN'          => 7,    //数据已回传
        'UPDATED'         => 8,    //数据已更新
        'REPLENISH'       => 9,    //已补货
    ];

    //邮件发送状态
    const IS_SEND_EMAIL = [
        'NO'  => 0,
        'YES' => 1
    ];

    //需要打印的仓库
    const PTINT_SID = [
        42,    //APM中转仓
        34     //CMS德国仓
    ];


    static function transportTypeList()
    {
        return config('yt.delivery.transport_type');
    }



    public function adminUser()
    {
        return $this->hasOne(User::class, 'id', 'act_user_id');
    }

    public function deliveryShortStock()
    {
        return $this->hasMany(DeliveryShortStock::class, 'delivery_id', 'id');
    }

    public static function getExternalNo($delivery_id)
    {
        return Cache::Remember('getExternalNo.' . $delivery_id, config('constants.LITTLE_CACHE_EXPIRES'), function () use ($delivery_id) {
            return self::query()->where(['id' => $delivery_id])->value('external_no');
        });
    }

    public function delivery(){
        return $this->hasOne(Delivery::class, 'id', 'parent_id');
    }
}
